package ce.com.cenewbluesdk.entity.k2;

import android.os.Parcel;
import android.os.Parcelable;

import java.nio.ByteBuffer;
import java.util.ArrayList;

import ce.com.cenewbluesdk.uitl.ByteParser;
import ce.com.cenewbluesdk.uitl.TimeUtil;

/**
 * typedef struct
 * {
 * unsigned char time[4];			//心率的起始时间
 * unsigned char heart_nums;      //心率数
 * unsigned char sustain_time[2];   //持续的时间，精确到秒
 * }heart_info_struct;
 * 心率信息中time为心率的计时起始时间，heart_nums为心率数，sustain_time为心率采样的时间。
 */
public class K2_HeartStruct implements Parcelable {

    long time;
    int heartNums;
    long sustainTime;

    public K2_HeartStruct() {

    }

    public K2_HeartStruct(byte[] bytes) {
        ByteParser parser = new ByteParser(bytes);
        time = TimeUtil.s2CForDev(parser.readInt(4), true).getTimeInMillis();
        heartNums = parser.readInt(1);
        sustainTime = parser.readInt(2);
//        int reserved = parser.readInt(1);// 保留字段
    }

    public byte[] getBytes() {
        byte[] bytes = new byte[7];

        int offset = 0;
        byte[] timeByte = ByteBuffer.allocate(4).putLong(time).array();
        System.arraycopy(timeByte, offset, bytes, 0, 4);
        offset += 4;

        bytes[offset++] = (byte) heartNums;

        byte[] sustainTimeByte = ByteBuffer.allocate(2).putLong(sustainTime).array();
        System.arraycopy(sustainTimeByte, offset, bytes, 0, 2);
        offset += 2;

        return bytes;
    }

    public K2_HeartStruct(long time, int heartNums, long sustainTime) {
        this.time = time;
        this.heartNums = heartNums;
        this.sustainTime = sustainTime;
    }

    protected K2_HeartStruct(Parcel in) {
        time = in.readLong();
        heartNums = in.readInt();
        sustainTime = in.readLong();
    }

    public static final Creator<K2_HeartStruct> CREATOR = new Creator<K2_HeartStruct>() {
        @Override
        public K2_HeartStruct createFromParcel(Parcel in) {
            return new K2_HeartStruct(in);
        }

        @Override
        public K2_HeartStruct[] newArray(int size) {
            return new K2_HeartStruct[size];
        }
    };

    public static ArrayList<K2_HeartStruct> parse(byte[] heartBytes) {
        ArrayList<K2_HeartStruct> list = new ArrayList<K2_HeartStruct>();
        ByteParser parser = new ByteParser(heartBytes);
        for (int i = 0; i < heartBytes.length / 7; i++) {
            list.add(new K2_HeartStruct(parser.readBytes(7)));
        }
        return list;
    }

    public long getTime() {
        return time;
    }

    public void setTime(long time) {
        this.time = time;
    }

    public int getHeartNums() {
        return heartNums;
    }

    public void setHeartNums(int heartNums) {
        this.heartNums = heartNums;
    }

    public long getSustainTime() {
        return sustainTime;
    }

    public void setSustainTime(long sustainTime) {
        this.sustainTime = sustainTime;
    }

    @Override
    public String toString() {
        return "Heart_struct_K3{" +
                "time=" + time +
                ", heartNums=" + heartNums +
                ", sustainTime=" + sustainTime +
                ", time=" + TimeUtil.long2String(time) +
                '}';
    }

    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeLong(time);
        dest.writeInt(heartNums);
        dest.writeLong(sustainTime);
    }
}
